package com.lzg.quest.controller;

import com.lzg.quest.pojo.QuesCommInfoDTO;
import com.lzg.quest.service.QuesCommInfoService;
import com.lzg.quest.utils.cheakNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * ex_liuzhigao
 * @Date: 2020/6/29 0026
 * beizhu
 */

@Controller
@RequestMapping("/quesPhone")
public class QuesCommInfoController {

    @Autowired
    private QuesCommInfoService comminfoService;
    
    private cheakNull cheakNullUtil;


    @RequestMapping(value = "getinfo.do",method = RequestMethod.GET)
    public String GetUser(Model model, HttpServletRequest request){
        String comm6 = request.getParameter("comm6");
        ArrayList<QuesCommInfoDTO> quesCommInfoDTOS = new ArrayList<QuesCommInfoDTO>();
        try {
            quesCommInfoDTOS = comminfoService.queryQuesCommInfo(comm6);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("infos",quesCommInfoDTOS);
        String returnStr = null;
        if("1".equals(comm6)){
            returnStr = "table";
        }else if("2".equals(comm6)){
            returnStr = "table2";
        }
        return returnStr;
    }

    @ResponseBody
    @RequestMapping("/insertInfo.do")
    public Map insertInfo(QuesCommInfoDTO qci){
        Map map = new HashMap();
        if("1".equals(qci.getComm6())){
            if(qci.getComm2() != null && !"".equals(qci.getComm2())){
                String[] is = qci.getComm2().split(",");
                qci.setImg1(is[0]);
                qci.setImg2(is[1]);
                qci.setImg3(is[2]);
            }
        }

        qci.setCreatetime(new Date());
        /*QuesCommInfoDTO quesCommInfoDTO = cheakNullUtil.cheakQuesCommInfoDTONull(qci);*/
        int rows = 0;
        try {
            rows = comminfoService.insertQuesCommInfo(qci);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(rows>0){
            map.put("state","1");
            map.put("msg","填写成功");
        }else{
            map.put("state","0");
            map.put("msg","服务器在开小差，稍后回来");
        }
        return map;
    }

    @RequestMapping("/empty.do")
    public ModelAndView showEmpty(Integer id) throws Exception {
        ModelAndView mav = new ModelAndView("empty");
        QuesCommInfoDTO queComm  = comminfoService.queryQuesDetilById(id);
        mav.addObject("queComm",queComm);
        return mav;
    }
    @RequestMapping("/empty2.do")
    public ModelAndView showEmpty2(Integer id) throws Exception {
        ModelAndView mav = new ModelAndView("empty2");
        QuesCommInfoDTO queComm  = comminfoService.queryQuesDetilById(id);
        mav.addObject("queComm",queComm);
        return mav;
    }

    @RequestMapping("/datescop.do")
    public String GetUserByDate(Model model,String date1,String date2, HttpServletRequest request) throws ParseException {
        String comm6 = request.getParameter("comm6");
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        String returnStr = "";
        Date d1 = format1.parse(date1);
        Date d2 = format1.parse(date2);
        ArrayList<QuesCommInfoDTO> quesCommInfoDTOS = new ArrayList<QuesCommInfoDTO>();
        try {
            quesCommInfoDTOS = comminfoService.queryQuesCommInfoByDate(d1,d2,comm6);
        } catch (Exception e) {
            e.printStackTrace();
        }
        model.addAttribute("infos",quesCommInfoDTOS);

        if("1".equals(comm6)){
            returnStr = "table";
        }else if("2".equals(comm6)){
            returnStr = "table2";
        }
        return returnStr;
    }


    @RequestMapping("/index.do")
    public String showMIndex(){
        return "Mindex";
    }
    @RequestMapping("/mindexCh.do")
    public String showForm(){ return "MindexCh"; }
    @RequestMapping("/admin.do")
    public String showPCIndex(){ return "index"; }





    @RequestMapping("/table.do")
    public String showTable(){ return "table"; }


}
